<template>
  <div>
    <el-form class="input-form"
             label-position="right"
             label-width="120px"
             ref="editForm"
             :rules="propRules"
             :disabled="propReadonly"
             :model="form"
    >
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="品名" prop="matrName">
            <matr-select
              v-model="form.matrId"
              :propRows="matrs"
              :propColumns="selectMatrColumns"
              :propSupplierId="propInitData.supplierId"
              :propDisabled="propReadonly"
              :propOmoc="propInitData.flagOmoc"
              :propConsignment="propInitData.poType.toUpperCase() == 'C'"
              ref="matrGrid"
              @select="handleMatrSelect"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="品规" prop="matrSpec">
            <el-input v-model="form.matrSpec" disabled/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="单品单位" prop="matrUnit">
            <el-input v-model="form.skuUnit" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="单品数量" prop="skuQty">
            <el-input v-model.number="form.skuQty" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="单品单价" prop="skuPrice">
            <el-input v-model.number="form.skuPrice" disabled/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="包装单位" prop="packageUnit">
            <matr-unit-select
              ref="matrUnitGrid"
              v-model="form.packageUnit"
              propUrl="/base/matrUnit/list"
              @select="handlePackageUnitChange"
              :propMatrId="form.matrId"
              propValueField="unit"
              :propDisabled="strings.isBlank(form.matrId)"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="包装数量" prop="packageQty">
            <el-input ref="qty" v-model.number="form.packageQty" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="包装比率" prop="packageRate">
            <el-input v-model.number="form.packageRate" disabled/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="小计(元)" prop="amount">
            <el-input v-model="form.amount" disabled/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" :rows="3"/>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
  </div>
</template>

<script>

import MatrSelect from '@/components/base/MatrSelect'
import MatrUnitSelect from '@/components/base/MatrUnitSelect'

export default {
  data() {
    return {
      form: {},
      viewData: {},
      packageUnits: [],
      matrs: [],
      selectMatrColumns: [
        { label: '品名', name: 'name', width: 200, search: true },
        { label: '品规',  name: 'spec', width: 150, search: true },
        { label: '厂商名称',  name: 'mfgName', search: true },
        { label: '单品单位',  name: 'skuUnit', width: 80, search: false },
        { label: '单品单价',  name: 'skuPrice', width: 80, search: false }
      ],
    }
  },
  methods: {
    handleMatrSelect(item) {
      let { name, spec, skuUnit, skuPrice, stockCount} = item
      this.$set(this.form, 'matrName', name)
      this.$set(this.form, 'matrSpec', spec)
      this.$set(this.form, 'skuUnit', skuUnit)
      this.$set(this.form, 'skuPrice', skuPrice)

      this.$set(this.form, 'packageUnit', null)
      this.$refs.matrUnitGrid.handleFocus()
    },
    fetchMatr() {
      if (!this.propParent.viewFormVisible) {
        return
      }

      let {supplierId, poType, flagOmoc} = this.propInitData
      if (this.strings.isBlank(supplierId)) {
        return
      }

      let wrapper = this.$newWrapper()
        .eq('supplierId', supplierId)
        .eq('flagConsignment', poType.toUpperCase() == 'C')
        .eq('flagOmoc', flagOmoc)
      let params = {
        queryString: JSON.stringify(wrapper.queryConditions)
      }
      this.$ajax.get('/base/matr/list', {params}).then(resp => {
        this.matrs = resp.data
      })
    },
    handlePackageUnitChange(item) {
      if (!item) {
        this.$set(this.form, 'packageUnit', null)
        this.$set(this.form, 'packageRate', null)
        return
      }
      this.$set(this.form, 'packageRate', item.rate)
      this.$set(this.form, 'packageQty', 1)
      this.$refs.qty.$el.querySelector('input').focus()
    }
  },
  computed: {
    cpMatrFetchCondition() {
      // 耗材数据提取条件
      let {supplierId, poType, flagOmoc} = this.propInitData
      let {viewFormVisible} = this.propParent

      return JSON.stringify({
        supplierId, poType, flagOmoc
      })
    },
    cpPackageQty() {
      return this.form.packageQty
    }
  },
  watch: {
    cpMatrFetchCondition(newValue, oldValue) {
      this.fetchMatr()
    },
    cpPackageQty() {
      let { packageRate, packageQty, skuPrice} = this.form
      this.$set(this.form, 'skuQty', (packageRate && packageQty) ? packageRate * packageQty : 1)
      this.$set(this.form, 'amount', (packageRate && packageQty && skuPrice) ? packageRate * packageQty * skuPrice : 0)
    }
  },
  created() {
    let that = this
    this.fetchMatr()
    this.$emit('event-register', {
      validate: (callback) => {
        this.$refs.editForm.validate(callback)
      },
      beforeSave: () => {
        this.$emit('data-callback', this.form)
        return true
      },
      afterMove: (formData, initData) => {
        this.form = global.utils.jsonClone(formData)
        this.$set(this.form, 'orderId', initData.id)
        if (!this.form.id) { //新增时直接弹出耗材选择框 { //新增时直接弹出耗材选择框
          this.$refs.matrGrid.handleFocus()
        }
        this.propParentPage.$emit('refresh-row')
      },
      afterSave: () => {
        this.propParentPage.$emit('refresh-row')
      },
      beforeDelete: () => {
        return true
      },
      afterDelete: () => {
        this.propParentPage.$emit('refresh-row')
      },
      beforeFlow: (flowButtonKey) => {
        return true
      },
      afterFlow: (flowButtonKey) => {
      },
      beforeClose: () => {
        return true
      }
    })
  },
  props: {
    propParent: Object,
    propInitData: Object,
    propPrimaryKey: String,
    propDictionaries: Object,
    propCurrentRow: Object,
    propViewFormData: Object,
    propReadonly: Boolean,
    propStatus: String,
    propRules: Object,
    propParentPage: Object
  },
  components: {
    MatrSelect,
    MatrUnitSelect
  }
}
</script>

<style lang="scss" scoped>
  .container {
    width: 100%;
    height: 100%;
  }
</style>
